from django.shortcuts import render
from django.contrib.auth.hashers import check_password
# Create your views here.
from rest_framework.views import APIView
from itsdangerous import TimedJSONWebSignatureSerializer
from rest_framework.response import Response
from .models import *
from . import serializer


class Register(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        mobile = request.data.get('mobile')
        weixinid = request.data.get('weixinid')
        email = request.data.get('email')
        print(username,password)
        data = {
            'username':username,
            'password':password,
            'mobile':mobile,
            'weixinid':weixinid,
            'email':email
        }
        user = User.objects.filter(username=username).first()
        # return Response({'code': 20011, 'data': '注册失败'})
        if user:
            return Response({'code':20010,'data':'该用户已存在'})
        user = serializer.UserSerializer(data=data)
        if user.is_valid():
            user.save()
            return Response({'code':200,'data':'ok'})
        return Response({'code':20011,'data':'注册失败'})

class Login(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        user  =User.objects.filter(username=username).first()
        if user:
            if check_password(password,user.password):
                t = TimedJSONWebSignatureSerializer('user')
                token = t.dumps({'uid':user.id}).decode()
                return Response({
                    'code':200,
                    'data':'ok',
                    'token':token
                })
            return Register({'code': 20011, 'data': '密码错误'})
        return Register({'code': 20012, 'data': '用户不存在'})


class getUserList(APIView):
    def get(self,request):
        username = request.GET.get('username')
        if username:
            user_data = User.objects.get(username=username)
            ur_list = []
            for i in user_data.role.all():
                ur_list.append(i.id)
            return Response({'code': 200, 'ur_list': ur_list})
        user_data = User.objects.all()
        user = serializer.UserModelSerializer(instance=user_data,many=True)
        return Response({'code':200,'users':user.data})

class DelUser(APIView):
    def post(self,requset):
        id = requset.data.get('id')
        user = User.objects.filter(pk=id).first()
        if user:
            User.objects.filter(pk=id).delete()
            return Response({'code':200})
        return Response({'code': 1})

class UpdateUser(APIView):
    def get(self,request):
        id = request.GET.get('id')
        user_data = User.objects.filter(id=id).first()
        user = serializer.UserModelSerializer(instance=user_data)
        return Response({'user':user.data})

    def post(self,request):
        ur = request.data.getlist('ur')
        if ur:
            username = request.data.get('username')
            user = User.objects.filter(username=username).first()
            user.role.set(ur)
            user.save()
            return Response({'code': 200})
        id = request.data.get('id')
        mobile = request.data.get('mobile')
        weixinid = request.data.get('weixinid')
        email = request.data.get('email')
        user = User.objects.filter(id=id).first()
        user.mobile = mobile
        user.weixinid = weixinid
        user.email = email
        user.save()
        return Response({'code':200})


class AddRole(APIView):
    def post(self,request):
        data = {
            'zh_name':request.data.get('zh_name'),
            'name':request.data.get('name'),
            'description':request.data.get('description')
        }
        role = serializer.RoleSerializer(data=data)
        if role.is_valid():
            role.save()
            return Response({'code':200})
        return Response({'code':1})

class AddDepar(APIView):
    def get(self,request):
        depar_data = Department.objects.all()
        depar = serializer.DeparModelSerializer(instance=depar_data,many=True)
        return Response({'depar':depar.data})


    def post(self,requset):
        data = {
            'name':requset.data.get('name'),
            'fid_id':requset.data.get('fid')
        }
        print(data)
        depar = Department(name='jkh')
        depar.save()
        print(depar)
        # depar = serializer.DeparSerializer(data=data)
        # if depar.is_valid():
        #     depar.save()
        #     return Response({'code':200})
        return Response({'code':1})



class RoleList(APIView):
    def get(self,request):
        role_data = Role.objects.all()
        role = serializer.RoleModelSerializer(role_data,many=True)
        return Response({'role':role.data})



